
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

// Global to track the number of active fires
NUM_FIRES 		= 0
MAX_NUM_FIRES 	= 128

function ENT:RemoveMe()

	if (!self.Entity) then return end
	if (self.Entity == NULL) then return end

	self.Entity:Remove()

end


/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self:SetMoveType( MOVETYPE_NONE )
	self:DrawShadow( false )

	// Make the bbox short so we can jump over it
	// Note that we need a physics object to make it call triggers
	self:SetCollisionBounds( Vector( -34, -34, -34 ), Vector( 34, 34, 0 ) )
	self:PhysicsInitBox( Vector( -34, -34, -34 ), Vector( 34, 34, 0 ) )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableCollisions( false )		
	end
	
	local Time = math.Rand( 5, 8 )
	timer.Simple( Time, self.RemoveMe, self )
	
	self:SetNetworkedFloat( 0, CurTime() + Time )
	
	NUM_FIRES = NUM_FIRES + 1
	
	self:SetCollisionGroup( COLLISION_GROUP_DEBRIS_TRIGGER )
	self:SetTrigger( true )
	self:SetNotSolid( true )
	
	local trace = {}
	trace.start = self:GetPos()
	trace.endpos = self:GetPos() + Vector( 0, 0, -1000 )
	trace.filter = self
	local tr =  util.TraceLine( trace )
	
	if tr.Entity and !tr.HitWorld then
		local trace2 = {}
		trace2.start = tr.Entity:GetPos()
		trace2.endpos = tr.Entity:GetPos() + Vector( 0, 0, -200 )
		trace2.filter = tr.Entity
		local tr2 =  util.TraceLine( trace2 )
		self:SetPos( (tr2.HitPos + tr2.HitNormal * 33) )
	else
		self:SetPos( (tr.HitPos + tr.HitNormal * 33) )
	end
	self:SetAngles( tr.HitNormal:Angle() )
end

/*---------------------------------------------------------
   Name: OnRemove
---------------------------------------------------------*/
function ENT:OnRemove()
	NUM_FIRES = NUM_FIRES - 1
end


/*---------------------------------------------------------
   Name: Touch
---------------------------------------------------------*/
function ENT:Touch( entity )
	if entity:IsPlayer() then 
		if not entity.IsInVehicle then
			entity:DoIgnite( self.Entity:GetOwner() )
		end
	end
end

include('shared.lua')